﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Oridea.Data.Fetching.Tests {
	[TestClass]
	public class CollectionFetcherTests {
		
		[TestMethod]
		public void CallingCount_Executes_Only_Count_Query() {
			MockCollection<string> strings = MakeMockCollection();
			var fetcher = new CollectionFetcher<string>(strings.AsQueryable());
			Assert.IsFalse(strings.HasCalledCount, "HasCalledCount shall be false before calling Count.");
			Assert.IsFalse(strings.HasCalledGetEnumerator, "HasCalledGetEnumerator shall be false.");
			var count = fetcher.Count();
			Assert.IsTrue(strings.HasCalledCount, "HasCalledCount shall be true after calling Count.");
			Assert.IsFalse(strings.HasCalledGetEnumerator, "HasCalledGetEnumerator shall be false after calling Count.");
		}

		[TestMethod]
		public void CallingFetch_Executes_Full_Query_With_Count() {
			MockCollection<string> strings = MakeMockCollection();
			var fetcher = new CollectionFetcher<string>(strings.AsQueryable());
			ReadOnlyCollection<string> fetchedStrings = fetcher.Fetch();
			int count = fetcher.Count();

			Assert.IsTrue(strings.HasCalledGetEnumerator);
			Assert.IsFalse(strings.HasCalledCount);
			Assert.AreEqual(count, strings.Count);

			for (int i = 0; i < strings.Count; i++) {
				Assert.AreEqual(strings[i], fetchedStrings[i]);
			}
		}

		public static MockCollection<string> MakeMockCollection() {
			return new MockCollection<string> { "Apple", "Orange", "Carrot" };
		}
	}
}
